﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Xml.Schema;
using System.IO;
using DataTransferObject;

namespace DataService.TestingService
{
    class FreeTimeBlockFromXML
    {
        public FreeTimeList readFreeTimeBlock(String XMLPath)
        {
            FreeTimeList freeTimeList = new FreeTimeList();
            XmlDocument xmldoc = new XmlDocument();
            //FileStream fs = new FileStream(@"D:\Jske\HOAN\FreeTimeBlock.xml", FileMode.Open, FileAccess.Read,
            //                   FileShare.ReadWrite);
            FileStream fs = new FileStream(XMLPath, FileMode.Open, FileAccess.Read,
                               FileShare.ReadWrite);
            xmldoc.Load(fs);

            XmlNodeList xmlnode = xmldoc.GetElementsByTagName("FreeTimeBlock");

            for (int i = 0; i < xmlnode.Count; i++)
            {
                FreeTimeBlock freeTimeBlock = new FreeTimeBlock();

                for (int j = 0; j < xmlnode[i].ChildNodes.Count; j++)
                {
                    string sName = xmlnode[i].ChildNodes[j].Name;
                    switch (sName)
                    {
                        case "startDateTime":
                            freeTimeBlock.StartDateTime = DateTime.ParseExact(xmlnode[i].ChildNodes[j].InnerText, "dd/MM/yyyy HH:mm", null);
                            break;
                        case "endDateTime":
                            freeTimeBlock.EndDateTime = DateTime.ParseExact(xmlnode[i].ChildNodes[j].InnerText, "dd/MM/yyyy HH:mm", null);
                            break;                        
                        case "ID":
                            freeTimeBlock.ID_ = xmlnode[i].ChildNodes[j].InnerText;
                            break;
                    }//end Switch

                    freeTimeList.addFreeTimeBlockToList(freeTimeBlock);
                }//end For
            }
            return freeTimeList;
        }//end readFreeTimeBlock
    }
}
